﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VirtualOrg.DB;
using VirtualOrg.Helper;
using System.Web.UI.HtmlControls;

namespace VirtualOrg.Web
{
    public partial class FormUpdateUser : OboutInc.oboutAJAXPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Request.Cookies.AllKeys.Contains(SiteConstants.CookieName))
            {
                Response.Redirect("~/User/Login.aspx");
            }

            if (!IsPostBack)
            {
                GetUserInfo();
                LoadDropDowns();
                GetWorkInfo();
                GetEducationInfo();
            }
        }

        private void GetUserInfo()
        {
            using (DbStoreDataContext dataContext = new DbStoreDataContext())
            {
                var result = dataContext.GetUserInfo(SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID);
                foreach (var item in result)
                {
                    txtName.Text = item.FullName;
                    txtEmail.Text = item.EmailID;
                    txtBirthday.Text = item.DateOfBirth.ToString("MM/dd/yyyy");
                    ddlSex.SelectedValue = item.Gender;
                    ddlCountry.SelectedValue = item.Country;
                    txtLocation.Text = item.Location;
                    txtContact.Text = item.ContactNo;
                    txtAlternateEmail.Text = item.AlternateEmailID;
                    lblTitle.InnerText = item.FullName;
                    txtOverview.Text = item.Overview;
                    imgProfilePic.Src = string.Format("../Content/Photos/ProfilePic/{0}", item.ProfilePic);
                }
            }
        }

        protected void btnUpdatePersonalInfo_Click(object sender, EventArgs e)
        {
            MessageBox msgbox = null;
            try
            {
                using (DbStoreDataContext dataContext = new DbStoreDataContext())
                {
                    dataContext.UpdateUserInfo(SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID,
                                                txtName.Text, Convert.ToDateTime(txtBirthday.Text), ddlSex.SelectedValue, ddlCountry.Text, txtLocation.Text,
                                                txtContact.Text, txtAlternateEmail.Text, txtOverview.Text);
                    msgbox = new MessageBox("Personal info updated!");
                }
            }
            catch
            {
                msgbox = new MessageBox("Oops!! Error occured.");
            }

            (this.Master.FindControl("msgboxpanel") as HtmlGenericControl).InnerHtml = msgbox.ReturnObject();
        }

        private void GetWorkInfo()
        {
            using (DbStoreDataContext dataContext = new DbStoreDataContext())
            {
                var result = dataContext.GetUserWorkInfo(SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID);
                GridEmployers.DataSource = result;
                GridEmployers.DataBind();
            }
        }

        private void GetEducationInfo()
        {
            using (DbStoreDataContext dataContext = new DbStoreDataContext())
            {
                var result = dataContext.GetUserEducationInfo(SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID);
                GridEducation.DataSource = result;
                GridEducation.DataBind();
            }
        }

        private void LoadDropDowns()
        {
            ddlExpYears.Items.Add(new ListItem("Years", ""));
            for (int i = 0; i < 35; i++)
            {
                ddlExpYears.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            int currentYear = DateTime.Now.Year;
            for (int i = currentYear; i >= currentYear - 35; i--)
            {
                ListItem listItem = new ListItem(i.ToString(), i.ToString());
                ddlStartYear.Items.Add(listItem);
                ddlEndYear.Items.Add(listItem);
                ddlCompletionYear.Items.Add(listItem);
            }
        }

        protected void lnkUpdateExp_Click(object sender, EventArgs e)
        {
            MessageBox msgbox = null;
            try
            {
                int? expYears = null;
                int? expMonths = null;

                if (!string.IsNullOrWhiteSpace(ddlExpYears.SelectedValue))
                {
                    expYears = Convert.ToInt32(ddlExpYears.SelectedValue);
                }

                if (!string.IsNullOrWhiteSpace(ddlExpMonths.SelectedValue))
                {
                    expMonths = Convert.ToInt32(ddlExpMonths.SelectedValue);
                }

                using (DbStoreDataContext dataContext = new DbStoreDataContext())
                {
                    dataContext.UpdateWorkInfo(SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID,
                                                expYears, expMonths, "");
                    msgbox = new MessageBox("Work experience updated!");
                }
            }
            catch
            {
                msgbox = new MessageBox("Oops!! Error occured.");
            }

            (this.Master.FindControl("msgboxpanel") as HtmlGenericControl).InnerHtml = msgbox.ReturnObject();
        }

        protected void btnAddEducation_Click(object sender, EventArgs e)
        {
            MessageBox msgbox = null;
            if (string.IsNullOrWhiteSpace(txtCollege.Text.Trim()) && string.IsNullOrWhiteSpace(ddlCompletionYear.SelectedValue.Trim()))
            {
                msgbox = new MessageBox("Please specify college and completion year");
                (this.Master.FindControl("msgboxpanel") as HtmlGenericControl).InnerHtml = msgbox.ReturnObject();
            }
            else
            {
                using (DbStoreDataContext dataContext = new DbStoreDataContext())
                {
                    dataContext.AddUserEducationInfo(SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID, txtCollege.Text, txtSpecialization.Text, Convert.ToInt32(ddlCompletionYear.SelectedValue));
                }

                GetEducationInfo();
            }
        }

        protected void btnAddEmployer_Click(object sender, EventArgs e)
        {
            MessageBox msgbox = null;
            if (string.IsNullOrWhiteSpace(txtCompanyName.Text.Trim()) && string.IsNullOrWhiteSpace(txtCompanyLocation.Text.Trim()))
            {
                msgbox = new MessageBox("Please specify college name and completion year");
                (this.Master.FindControl("msgboxpanel") as HtmlGenericControl).InnerHtml = msgbox.ReturnObject();
            }
            else
            {
                string startDate = "", endDate = "";
                if (!string.IsNullOrWhiteSpace(ddlStartYear.SelectedValue))
                {
                    startDate = string.Format("{0} - {1}", ddlStartMonth.SelectedValue, ddlStartYear.SelectedValue);
                }

                if (!string.IsNullOrWhiteSpace(ddlEndYear.SelectedValue))
                {
                    endDate = string.Format("{0} - {1}", ddlEndMonth.SelectedValue, ddlEndYear.SelectedValue);
                }

                using (DbStoreDataContext dataContext = new DbStoreDataContext())
                {
                    dataContext.AddUserWorkInfo(SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID, txtCompanyName.Text, txtCompanyLocation.Text, startDate, endDate);
                }

                GetWorkInfo();
            }
        }

        protected void imgDeleteEmployer_Click(object sender, ImageClickEventArgs e)
        {
            using (DbStoreDataContext dataContext = new DbStoreDataContext())
            {
                dataContext.DeleteUserWorkInfo(Convert.ToInt64((sender as ImageButton).Attributes["wid"]), SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID);
            }

            GetWorkInfo();
        }

        protected void imgDeleteCollege_Click(object sender, ImageClickEventArgs e)
        {
            using (DbStoreDataContext dataContext = new DbStoreDataContext())
            {
                dataContext.DeleteUserEducationInfo(Convert.ToInt64((sender as ImageButton).Attributes["eid"]), SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID);
            }

            GetEducationInfo();
        }

        protected void btnChangePassword_Click(object sender, EventArgs e)
        {
            MessageBox msgbox = null;
            try
            {
                if (txtNewPassword.Text != txtRetypeNewPassword.Text)
                {
                    msgbox = new MessageBox("Password mismatch!");
                }

                using (DbStoreDataContext dataContext = new DbStoreDataContext())
                {
                    if (txtOldPassword.Text == dataContext.GetUserPasswordByUserID(SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID))
                    {
                        dataContext.UpdateUserPassword(SiteHelper.DecryptSession(Request.Cookies[SiteConstants.CookieName].Value).UserID, txtNewPassword.Text);
                    }
                    else
                    {
                        msgbox = new MessageBox("Invalid current password!");
                    }
                }
            }
            catch
            {
                msgbox = new MessageBox(WebResource.ErrorMessage);
            }
        }

    }
}